﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using LogisticsAndong.Business;
using LogisticsAndong.Model;

namespace LogisticsAndong.Forms
{
    public partial class FrmExceptionRecord : UIPage
    {
        private SystemDeviceMonitorBLL bllSystemDeviceMonitor = new SystemDeviceMonitorBLL();
        private PlanMonitorBLL bllLogisticsSystemPlanMonitor = new PlanMonitorBLL();
        List<SystemDeviceMonitorModel> SystemDeviceMonitorList = new List<SystemDeviceMonitorModel>();
        List<PlanMonitorModel> LogisticsSystemPlanMonitorList = new List<PlanMonitorModel>();
        
        /// <summary>
        /// 判断窗体是否首次加载
        /// </summary>
        bool isLoad = true;
        /// <summary>
        /// 数据表每页行数
        /// </summary>
        int pageSize = 30;       
        public FrmExceptionRecord()
        {
            InitializeComponent();
        }

        private void FrmExceptionRecord_Load(object sender, EventArgs e)
        {
            //设置开始时间为两天前
            this.dpJDStartDate.Value = DateTime.Now.AddDays(-2);
            this.dpJDEndDate.Value = DateTime.Now;

            //设置开始时间为一星期前
            this.dpStartDate.Value = DateTime.Now.AddDays(-7);
            this.dpEndDate.Value = DateTime.Now;

            if (!isLoad)
                return;
            isLoad = false;

            uiPagination1.Style = UIStyle.Blue;
            uiPagination1.ShowJumpButton = true;
            uiPagination2.Style = UIStyle.Blue;
            uiPagination2.ShowJumpButton = true;

            dgvHistory.AutoGenerateColumns = false;
            dgvHistory.AllowUserToAddRows = false;
            dgvHistory.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHistory.ReadOnly = true;
            dgvHistory.AddColumn("时间", "ExceptionDate", 25);
            dgvHistory.AddColumn("设备类型", "DeviceType", 25);
            dgvHistory.AddColumn("设备名称", "DeviceName", 25);
            dgvHistory.AddColumn("异常描述", "ExceptionDescription", 25);

            SystemDeviceMonitorList = bllSystemDeviceMonitor.GetModelListByCondition(dpStartDate.Value, dpEndDate.Value,"");

            //设置分页控件总数
            uiPagination1.TotalCount = SystemDeviceMonitorList.Count;
            //设置分页控件每页数量
            uiPagination1.PageSize = pageSize;

            dgvHistoryJD.AutoGenerateColumns = false;
            dgvHistoryJD.AllowUserToAddRows = false;
            dgvHistoryJD.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHistoryJD.ReadOnly = true;
            dgvHistoryJD.AddColumn("记录时间", "CreateTime", 12);
            dgvHistoryJD.AddColumn("部品名称", "ProductName", 14);
            dgvHistoryJD.AddColumn("开捆类型", "AreaName", 12);
            dgvHistoryJD.AddColumn("开始时间", "StartTime", 12);
            dgvHistoryJD.AddColumn("结束时间", "EndTime", 12);
            dgvHistoryJD.AddColumn("实际开始时间", "WorkStartTime", 12);
            dgvHistoryJD.AddColumn("实际结束时间", "WorkEndTime", 12);
            dgvHistoryJD.AddColumn("异常描述", "Description", 14);

            LogisticsSystemPlanMonitorList = bllLogisticsSystemPlanMonitor.GetModelList();

            //设置分页控件总数
            uiPagination2.TotalCount = LogisticsSystemPlanMonitorList.Count;
            //设置分页控件每页数量
            uiPagination2.PageSize = pageSize;
        }

        private void FrmExceptionRecord_SizeChanged(object sender, EventArgs e)
        {

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string deviceType = "";
            DateTime startDate = dpStartDate.Value;
            DateTime endDate = dpEndDate.Value;
            if (!cbdeviceType.Text.Equals("全部"))
            {
                deviceType = cbdeviceType.Text;
            }
            SystemDeviceMonitorList = bllSystemDeviceMonitor.GetModelListByCondition(startDate, endDate, deviceType);
            uiPagination1.TotalCount = SystemDeviceMonitorList.Count;
            uiPagination1_PageChanged(null, null, 1, pageSize);
        }

        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            //一般通过ORM的分页去取数据来填充
            //pageIndex：第几页，和界面对应，从1开始，取数据可能要用pageIndex - 1
            //count：单页数据量，也就是PageSize值
            List<SystemDeviceMonitorModel> data = new List<SystemDeviceMonitorModel>();
            for (int i = (pageIndex - 1) * count; i < (pageIndex - 1) * count + count; i++)
            {
                if (i >= SystemDeviceMonitorList.Count) continue;
                data.Add(SystemDeviceMonitorList[i]);
            }
            dgvHistory.DataSource = data;
        }

        private void uiPagination2_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            //一般通过ORM的分页去取数据来填充
            //pageIndex：第几页，和界面对应，从1开始，取数据可能要用pageIndex - 1
            //count：单页数据量，也就是PageSize值
            List<PlanMonitorModel> data = new List<PlanMonitorModel>();
            for (int i = (pageIndex - 1) * count; i < (pageIndex - 1) * count + count; i++)
            {
                if (i >= LogisticsSystemPlanMonitorList.Count) continue;
                data.Add(LogisticsSystemPlanMonitorList[i]);
            }
            dgvHistoryJD.DataSource = data;
        }

        private void btnQueryJD_Click(object sender, EventArgs e)
        {
            string jdType = "";
            DateTime startDate = dpJDStartDate.Value;
            DateTime endDate = dpJDEndDate.Value;
            if (!cbJdType.Text.Equals("全部"))
            {
                jdType = cbJdType.Text;
            }
            LogisticsSystemPlanMonitorList = bllLogisticsSystemPlanMonitor.GetModelListByCondition(startDate, endDate, jdType);
            uiPagination2.TotalCount = LogisticsSystemPlanMonitorList.Count;
            uiPagination2_PageChanged(null, null, 1, pageSize);
        }

    }
}
